package com.ru.application.creditscore.service;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import com.ru.application.creditscore.dao.ScoringRuleCaseDao;
import com.ru.application.creditscore.dao.support.SearchMode;
import com.ru.application.creditscore.dao.support.SearchParameters;
import com.ru.application.creditscore.domain.ScoringRuleCase;
import com.ru.application.creditscore.domain.ScoringRule;

public class ScoringRuleCaseServiceImpl implements ScoringRuleCaseService {
    
    private final static Logger logger = Logger.getLogger(ScoringRuleCaseServiceImpl.class);
    private ScoringRuleCaseDao ruleCaseDao;
    
    public ScoringRuleCase findRuleCase(Integer id) {
	if (logger.isDebugEnabled()) {
		logger.debug("Find ruleCase with Id = " + id.toString());
	}
	ScoringRuleCase ruleCase = new ScoringRuleCase();
	try {
	    	ruleCase.setId(id);
		SearchParameters sp = new SearchParameters();
		sp.searchMode(SearchMode.EQUALS);
		ruleCase = ruleCaseDao.findUnique(ruleCase, sp);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Find ruleCase OK with Id = " + id.toString());
	}
	return ruleCase;
    }	
	
    public List<ScoringRuleCase> findAll(SearchParameters sp) {
	if (logger.isDebugEnabled()) {
		logger.debug("Find all ruleCase");
	}
	List<ScoringRuleCase> listRuleCase = new ArrayList<ScoringRuleCase>();
	ScoringRuleCase ruleCase = new ScoringRuleCase();
	try {
	    listRuleCase = ruleCaseDao.find(ruleCase, sp);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Find all ruleCase OK");
	}
	return listRuleCase;
    }

    public List<ScoringRuleCase> findRuleCaseByRuleId(Integer ruleId, SearchParameters sp) {
	if (logger.isDebugEnabled()) {
		logger.debug("Find all ruleCase by RuleId = " + ruleId.toString());
	}
	List<ScoringRuleCase> listRuleCase = new ArrayList<ScoringRuleCase>();
	ScoringRuleCase ruleCase = new ScoringRuleCase();
	try {
	    ScoringRule rule = new ScoringRule();
	    rule.setId(ruleId);
	    ruleCase.setRule(rule);
	    listRuleCase = ruleCaseDao.find(ruleCase, sp);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Find all ruleCase by RuleId OK");
	}
	return listRuleCase;
    }
    
    @Transactional
    public boolean updateRuleCase(ScoringRuleCase pRuleCase) {
	if (logger.isDebugEnabled()) {
		logger.debug("Update ruleCase with id = " + pRuleCase.getId().toString());
	}
	boolean result = false;
	ScoringRuleCase ruleCase = new ScoringRuleCase();
	try {
	    ruleCase = ruleCaseDao.merge(pRuleCase);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled() && ruleCase != null) {
		logger.debug("Update ruleCase OK");
		result = true;
	}
	return result;
    }

    @Transactional
    public void createRuleCase(ScoringRuleCase ruleCase) {
	if (logger.isDebugEnabled()) {
		logger.debug("create ruleCase");
	}

	try {
	    ruleCaseDao.save(ruleCase);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("create ruleCase OK");
	}
    }
    
    @Transactional
    public void removeRuleCase(Integer id) {
	if (logger.isDebugEnabled()) {
		logger.debug("remove ruleCase");
	}
	ScoringRuleCase rule = new ScoringRuleCase();
	rule.setId(id);
	try {
	    ruleCaseDao.delete(rule);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("remove ruleCase OK");
	}
    }
    
    @Transactional
    public void updateListRuleCase(List<ScoringRuleCase> listRuleCase) {
	if (logger.isDebugEnabled()) {
		logger.debug("updateListRuleCase");
	}

	try {
		for (ScoringRuleCase rule : listRuleCase)
		    ruleCaseDao.merge(rule);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("updateListRuleCase OK");
	}
    }
    
    @Transactional
    public void createListRuleCase(List<ScoringRuleCase> listRuleCase) {
	if (logger.isDebugEnabled()) {
		logger.debug("createListRuleCase");
	}

	try {
		for (ScoringRuleCase ruleCase : listRuleCase)
		    ruleCaseDao.save(ruleCase);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("createListRuleCase OK");
	}
    }
    
    @Transactional
    public void removeListRuleCase(List<Integer> listId) {
	if (logger.isDebugEnabled()) {
		logger.debug("removeListRuleCase");
	}

	try {
		for (Integer id : listId) {
		    	ScoringRuleCase rule = new ScoringRuleCase();
		    	rule.setId(id);
		        ruleCaseDao.delete(rule);
		}
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB, ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("removeListRuleCase OK");
	}
    }

    public ScoringRuleCaseDao getRuleCaseDao() {
        return ruleCaseDao;
    }

    public void setRuleCaseDao(ScoringRuleCaseDao ruleCaseDao) {
        this.ruleCaseDao = ruleCaseDao;
    }
    
}
